package com.sendingFBpost;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;  
import android.widget.Toast;

// checking the update by clicking update version

/*import com.fbexample.DialogError;
import com.fbexample.Facebook;
import com.fbexample.Facebook.DialogListener;
import com.fbexample.FacebookError;    
*/     
public class FBAppActivity extends Activity {  
// private static final String APP_ID = "434327979941439";
 private static final String APP_ID = "268084823307896";  
 private static final String[] PERMISSIONS = new String[]
  { "publish_stream" };
        
 private static final String TOKEN = "access_token";
 private static final String EXPIRES = "expires_in";
 private static final String KEY = "facebook-credentials";
 
 public  Facebook facebook;    
 private String messageToPost; 

 public boolean saveCredentials(Facebook facebook) {
  Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
  editor.putString(TOKEN, facebook.getAccessToken());
  editor.putLong(EXPIRES, facebook.getAccessExpires());
  return editor.commit();
 }

 public boolean restoreCredentials(Facebook facebook) {
  SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
  facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
  facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
  return facebook.isSessionValid();
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  facebook = new Facebook(APP_ID);
  restoreCredentials(facebook);

  requestWindowFeature(Window.FEATURE_NO_TITLE);

  setContentView(R.layout.facebook_sample); 

  String facebookMessage = getIntent().getStringExtra("facebookMessage");
  if (facebookMessage == null) {
   facebookMessage = "Test wall post";
  }
  messageToPost = facebookMessage;
 }

 public void doNotShare(View button) {
  finish();
 }

 public void share(View button) {
  if (!facebook.isSessionValid()) {
   loginAndPostToWall(); 
  } else {
   postToWall(messageToPost);
  }
 }

 public void loginAndPostToWall() {
  facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
 }

 public void postToWall(String message) {
//  Bundle parameters = new Bundle();
//  parameters.putString("message", message);
//  parameters.putString("description", "topic share");
//  try { 
//   //facebook.request("me");
//   String response = facebook.request("me/feed", parameters, "POST");
//   Log.d("Tests", "got response: " + response);
//   if (response == null || response.equals("") || response.equals("false")) {
//    showToast("Blank response."); 
//   } else { 
//    showToast("Message posted to your facebook wall!");
//   }
//   finish();
//  } catch (Exception e) {
//   showToast("Failed to post to wall!");
//   e.printStackTrace();
//   finish();
//  } 
	// MyAsyncTask123 mst=(MyAsyncTask123) new MyAsyncTask123().execute();
	 new MyAsyncTask123().execute();
 } 

 class LoginDialogListener implements DialogListener {
  public void onComplete(Bundle values) { 
   saveCredentials(facebook);
   if (messageToPost != null)  {
    postToWall(messageToPost);
   }
  }

  public void onFacebookError(FacebookError error) {
   showToast("Authentication with Facebook failed!");
   finish();
  }

  public void onError(DialogError error) {
   showToast("Authentication with Facebook failed!");
   finish();
  }

  public void onCancel() {
   showToast("Authentication with Facebook cancelled!");
   finish();
  }    
 }

 private void showToast(String message) {
  Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
 }    

 @Override
 protected void onStop() { 
     super.onStop();  // Always call the superclass method first
     showToast("in onstop method");
 }
 
 public void publishPhoto(byte[] imgData, String message) {
 
	  AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner (facebook);

	  Bundle parameters = new Bundle (); 
	  parameters.putString (Facebook.TOKEN, facebook.getAccessToken());
	  parameters.putString ("message", message);
	  parameters.putByteArray ("source", imgData);

	//  mAsyncRunner.Request ("me/photos", parameters, "POST", new RequestListener(), null);
	  } 
 
 // inner class 
 class MyAsyncTask123 
 extends AsyncTask<Void, Void, Void>
 {
 //private Facebook facebook;
    // ProgressDialog mProgressDialog;
     @Override
     protected void onPostExecute(Void result) {
         //mProgressDialog.dismiss();
     }

     @Override
     protected void onPreExecute() {
       //  mProgressDialog = ProgressDialog.show(ActivityName.this, "Loading...", "Data is Loading...");
     	//Facebook fb=new Facebook(); 
     	//FBAppActivity fb=new FBAppActivity();   
     	
     }

     @Override 
     protected Void doInBackground(Void... params) {
        // your network operation
    	 Bundle parameters = new Bundle();
    	  parameters.putString("message", "testing SVN Project now");
    	// parameters.putString("link", "http://115.118.114.27:88/MerchandiseAquisition/Favorites%20--%20Have%20not%20heard%20recently.wpl");
    	//  parameters.putString("picture", "http://simplestrength.com/wp-content/uploads/kanji_luck.png");
    	  parameters.putString("description", "topic share");
    	  try {     
    		  facebook.request("me");
    	   String response = facebook.request("me/feed", parameters, "POST");
    	   Log.d("Tests", "got response: " + response);
    	   if (response == null || response.equals("") || response.equals("false")) {
    	    showToast("Blank response.");  
    	   } else { 
    	    showToast("Message posted to your facebook wall!");
    	    Log.d("Sucess", "Message posted to your facebook wall!");
    	   }
    	  // finish();
    	  } catch (Exception e) {
    	   showToast("Failed to post to wall!"); 
    	   e.printStackTrace();
    	   //finish();     
    	  } 
         return null;
     }        private void showToast(String message) {
    	 // Toast.makeText(MyAsyncTask.this, message, Toast.LENGTH_SHORT).show();
     	//AlertDialog.Builder al=new AlertDialog.Builder(MyAsync);
   	 }

 }

}

 